// LucciValidator is an open-source project

package org.lucciframework.validator {
    
	/**
	 * 
	 * 
	 */
	
	import mx.core.IMXMLObject;
    import mx.events.ValidationResultEvent;
    import mx.validators.ValidationResult;

    import org.lucciframework.validator.core.AbstractValidator;

    public class Validators implements IMXMLObject, IValidators {

        private var _dataProvider : Array;

        [Bindable] public function get dataProvider() : Array {
            return _dataProvider;
        }

        public function set dataProvider(value : Array) : void {
            _dataProvider = value;
        }


        /**
         * @private
         */
        private function validateAll() : void {

            for each (var validationItem : AbstractValidator in _dataProvider) {

                validationItem.validate();
            }
        }

        public function checkValidator() : Boolean {

            var noValid : Boolean;

            validateAll();

            for each (var validationItem : AbstractValidator in _dataProvider) {

                for each (var validationResultEvent : ValidationResultEvent in validationItem.validationResultEventArray) {

                    if (validationResultEvent.results != null) {
                        validationItem.target.errorString =
                            ValidationResult(validationResultEvent.results[0]).errorMessage;
                    } else {
                        validationItem.target.errorString = "";
                    }

                    if (validationResultEvent.type == ValidationResultEvent.INVALID) {
                        noValid = true;
                        break;
                    }
                }
            }

            return noValid;
        }

        /**
         * @private
         */
        public function initialized(document : Object, id : String) : void {
        }


    }
}